package Tarea5arraylist;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class ClaPanel extends JPanel implements MouseListener{
	private static final int ANCHO = 64;
	private static final int ALTO = 140;
	private BufferedImage biSerieImagen;
	private Motor motor;
	private int opcion;
	private Random rnd=new Random();
	public ClaPanel(Motor motor){
		this.motor = motor;
		this.setSize(600, 600);		
		this.cargaSerie();
	}

	private void cargaSerie() {
		try{
			biSerieImagen = ImageIO.read(getClass().getClassLoader().getResource("cam.jpg"));
		}catch(Exception e){
			
		}
		
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, 600, 600);
		dibujaExplociones(g);
	}

	private void dibujaExplociones(Graphics g) {
		opcion=rnd.nextInt(8);
		if(motor.obtieneSerieDibujos().size() != 0){
			ArrayList aux = motor.obtieneSerieDibujos();			
			for(int cont=0; cont<aux.size(); cont++){
				ClaSeries e = (ClaSeries) aux.get(cont);
				g.drawImage(biSerieImagen, e.posX, e.posY, e.posX+ANCHO, e.posY+ALTO, e.obtieneNumDibujo()*ANCHO, 0, e.obtieneNumDibujo()*ANCHO+ANCHO, ALTO, null);				
				switch(opcion){
				case 1: 
					e.posX--;
					e.posY++;
					break;
				case 2: 
					e.posY++;
					break;
				case 3:  
					e.posX++;
					e.posY++;
					break;
				case 4:  
					e.posX++;
					break;
				case 5:  
					e.posX++;
					e.posY--;
					break;
				case 6: 
					e.posY--;
					break;
				case 7:  
					e.posX--;
					e.posY--;
					break;
				case 8: 
					e.posX--;
					break;
				}				
			}
		}
		
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent me) {
		motor.obtieneSerieDibujos().add(new ClaSeries(me.getX(), me.getY(), motor));
		//me.getX() obtiene poicion en x cuando hace click
		//me.getY() obtiene poicion en y cuando hace click
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}
